<?php

namespace App\Jobs;

use App\Order;
use App\User;
use App\UserCoupon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class CancelOrderJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $order_number;

    /**
     * Create a new job instance.
     *
     * @param $order_number
     */
    public function __construct($order_number)
    {
        $this->order_number=$order_number;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order=Order::where('order_number',$this->order_number)->first();
        if ($order&&$order->status==0){
            $order->status=-1;
            $order->save();
            if ($order->coupon_id){
                $coupons=explode(",",$order->coupon_id);
                foreach ($coupons as $value){
                    UserCoupon::where('id',$order->$value)->update(['status'=>0]);
                }

            }
        }
    }

    /**
     * @param float|int $delay
     */
    public function setDelay($delay): void
    {
        $this->delay = $delay;
    }
}
